#include "zd60_control.h"
#include "ui_zd60_control.h"
#include "Parameter.h"
#include "mainwindow.h"
#include <QMessageBox>

ZD60_Control::ZD60_Control(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ZD60_Control)
{
    ui->setupUi(this);
    ui->label_LED2->setStyleSheet("border-radius: " + QString::number(ui->label_LED2->width()/2) + "px; background-color: red;");
    ui->label_LED4->setStyleSheet("border-radius: " + QString::number(ui->label_LED4->width()/2) + "px; background-color: red;");
    ui->label_LED5->setStyleSheet("border-radius: " + QString::number(ui->label_LED5->width()/2) + "px; background-color: red;");
    ui->label_LED6->setStyleSheet("border-radius: " + QString::number(ui->label_LED6->width()/2) + "px; background-color: red;");
    ui->label_LED7->setStyleSheet("border-radius: " + QString::number(ui->label_LED7->width()/2) + "px; background-color: red;");
    ui->label_LED8->setStyleSheet("border-radius: " + QString::number(ui->label_LED8->width()/2) + "px; background-color: red;");
    ui->label_LED9->setStyleSheet("border-radius: " + QString::number(ui->label_LED9->width()/2) + "px; background-color: red;");
    ui->label_LED10->setStyleSheet("border-radius: " + QString::number(ui->label_LED10->width()/2) + "px; background-color: red;");
    ui->label_LED11->setStyleSheet("border-radius: " + QString::number(ui->label_LED11->width()/2) + "px; background-color: red;");
    ui->label_LED12->setStyleSheet("border-radius: " + QString::number(ui->label_LED12->width()/2) + "px; background-color: red;");
    ui->label_LED1->setStyleSheet("border-radius: " + QString::number(ui->label_LED1->width()/2) + "px; background-color: red;");
    ui->label_LED3->setStyleSheet("border-radius: " + QString::number(ui->label_LED3->width()/2) + "px; background-color: red;");
    ui->label_LED13->setStyleSheet("border-radius: " + QString::number(ui->label_LED13->width()/2) + "px; background-color: red;");
    ui->PodButton->setStyleSheet("background-color: red;");
    ui->CTButton->setStyleSheet("background-color: red;");
    ui->FDJButton->setStyleSheet("background-color: red;");

}




ZD60_Control::~ZD60_Control()
{
    delete ui;
}

void ZD60_Control::send_command_packet(uint8_t cmd)
{
    QByteArray packet;
    unsigned short Modbus_crc16 =0;
    packet.append((char)0xe9);
    packet.append((char)0x90);  // 包头
    packet.append((char)0x01);  // 板子ID
    packet.append((char)0x00);
    packet.append((char)(cmd&0xff));
    packet.append((char)(0x67));  // 数据类型
    unsigned short dala_len = 8;
    packet.append((char)(dala_len>>8));
    packet.append((char)(dala_len>>0));
    Modbus_crc16 = modbusCRC16(packet,dala_len);
    packet.append((char)(Modbus_crc16>>8));
    packet.append((char)(Modbus_crc16>>0));
    if (serialPort->isOpen())
    {
        // 通过串口写数据
        serialPort->write(packet);
    }
    else
    {
        QMessageBox::critical(this, "错误", "串口未打开，无法发送数据。");
    }
}

void ZD60_Control::send_statue_command(uint8_t cmd)
{
    QByteArray packet;
    unsigned short Modbus_crc16 =0;
    packet.append((char)0xe9);
    packet.append((char)0x90);  // 包头
    packet.append((char)0x01);  // 板子ID
    packet.append((char)0x00);
    packet.append((char)(cmd&0xff));
    packet.append((char)(0x66));  // 数据类型
    unsigned short dala_len = 8;
    packet.append((char)(dala_len>>8));
    packet.append((char)(dala_len>>0));
    Modbus_crc16 = modbusCRC16(packet,dala_len);
    packet.append((char)(Modbus_crc16>>8));
    packet.append((char)(Modbus_crc16>>0));
    if (serialPort->isOpen())
    {
        // 通过串口写数据
        serialPort->write(packet);
    }
    else
    {
        QMessageBox::critical(this, "错误", "串口未打开，无法发送数据。");
    }
}


void ZD60_Control::show_ZD60_Motor_statue(uint8_t Motor_type,uint8_t statue)
{

    switch(Motor_type)
    {

        case ZJB1_flag:

            if(statue == 0x01)
            {
                if(ZJB1_ShowLEDStatus)
                {
                    qDebug()<<"statue=1";
                    // 初始设置为红色
                    ui->label_LED1->setStyleSheet("border-radius: " + QString::number(ui->label_LED1->width()/2) + "px; background-color: red;");
                    ZJB1_ShowLEDStatus = false;
                }
                else
                {
                    qDebug()<<"statue=1";
                    // 初始设置为绿色
                    ui->label_LED1->setStyleSheet("border-radius: " + QString::number(ui->label_LED1->width()/2) + "px; background-color: green;");
                    ZJB1_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                qDebug()<<"statue=2";
                ui->label_LED1->setStyleSheet("border-radius: " + QString::number(ui->label_LED1->width()/2) + "px; background-color: green;");
            }
            else if(statue == 0x03)
            {
                qDebug()<<"statue=3";
                ui->label_LED1->setStyleSheet("border-radius: " + QString::number(ui->label_LED1->width()/2) + "px; background-color: red;");
            }
            break;
        case ZJB2_flag:
            if(statue == 0x01)
            {
                if(ZJB2_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED2->setStyleSheet("border-radius: " + QString::number(ui->label_LED2->width()/2) + "px; background-color: red;");
                    ZJB2_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED2->setStyleSheet("border-radius: " + QString::number(ui->label_LED2->width()/2) + "px; background-color: blue;");
                    ZJB2_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED2->setStyleSheet("border-radius: " + QString::number(ui->label_LED2->width()/2) + "px; background-color: blue;");
            }
            else if(statue == 0x03)
            {
                ui->label_LED2->setStyleSheet("border-radius: " + QString::number(ui->label_LED2->width()/2) + "px; background-color: red;");
            }
            break;
        case YJB1_flag:
            if(statue == 0x01)
            {
                if(YJB1_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED3->setStyleSheet("border-radius: " + QString::number(ui->label_LED3->width()/2) + "px; background-color: red;");
                    YJB1_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED3->setStyleSheet("border-radius: " + QString::number(ui->label_LED3->width()/2) + "px; background-color: green;");
                    YJB1_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED3->setStyleSheet("border-radius: " + QString::number(ui->label_LED3->width()/2) + "px; background-color: green;");
            }
            else if(statue == 0x03)
            {
                ui->label_LED3->setStyleSheet("border-radius: " + QString::number(ui->label_LED3->width()/2) + "px; background-color: red;");
            }
            break;

        case YJB2_flag:
            if(statue == 0x01)
            {
                if(YJB2_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED4->setStyleSheet("border-radius: " + QString::number(ui->label_LED4->width()/2) + "px; background-color: red;");
                    YJB2_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED4->setStyleSheet("border-radius: " + QString::number(ui->label_LED4->width()/2) + "px; background-color: blue;");
                    YJB2_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED4->setStyleSheet("border-radius: " + QString::number(ui->label_LED4->width()/2) + "px; background-color: blue;");
            }
            else if(statue == 0x03)
            {
                ui->label_LED4->setStyleSheet("border-radius: " + QString::number(ui->label_LED4->width()/2) + "px; background-color: red;");
            }
            break;
        case ZFYC1_flag:
            if(statue == 0x01)
            {
                if(ZFYC1_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED5->setStyleSheet("border-radius: " + QString::number(ui->label_LED5->width()/2) + "px; background-color: red;");
                    ZFYC1_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED5->setStyleSheet("border-radius: " + QString::number(ui->label_LED5->width()/2) + "px; background-color: green;");
                    ZFYC1_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED5->setStyleSheet("border-radius: " + QString::number(ui->label_LED5->width()/2) + "px; background-color: green;");
            }
            else if(statue == 0x03)
            {
                ui->label_LED5->setStyleSheet("border-radius: " + QString::number(ui->label_LED5->width()/2) + "px; background-color: red;");
            }
            break;
        case ZFYC2_flag:
            if(statue == 0x01)
            {
                if(ZFYC2_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED6->setStyleSheet("border-radius: " + QString::number(ui->label_LED6->width()/2) + "px; background-color: red;");
                    ZFYC2_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED6->setStyleSheet("border-radius: " + QString::number(ui->label_LED6->width()/2) + "px; background-color: blue;");
                    ZFYC2_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED6->setStyleSheet("border-radius: " + QString::number(ui->label_LED6->width()/2) + "px; background-color: blue;");
            }
            else if(statue == 0x02)
            {
                ui->label_LED6->setStyleSheet("border-radius: " + QString::number(ui->label_LED6->width()/2) + "px; background-color: red;");
            }
            break;
        case YFYC1_flag:
            if(statue == 0x01)
            {
                if(YFYC1_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED7->setStyleSheet("border-radius: " + QString::number(ui->label_LED7->width()/2) + "px; background-color: red;");
                    YFYC1_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED7->setStyleSheet("border-radius: " + QString::number(ui->label_LED7->width()/2) + "px; background-color: green;");
                    YFYC1_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED7->setStyleSheet("border-radius: " + QString::number(ui->label_LED7->width()/2) + "px; background-color: green;");
            }
            else if(statue == 0x03)
            {
                ui->label_LED7->setStyleSheet("border-radius: " + QString::number(ui->label_LED7->width()/2) + "px; background-color: red;");
            }
            break;
        case YFYC2_flag:
            if(statue == 0x01)
            {
                if(YFYC2_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED8->setStyleSheet("border-radius: " + QString::number(ui->label_LED8->width()/2) + "px; background-color: red;");
                    YFYC2_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED8->setStyleSheet("border-radius: " + QString::number(ui->label_LED8->width()/2) + "px; background-color: blue;");
                    YFYC2_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED8->setStyleSheet("border-radius: " + QString::number(ui->label_LED8->width()/2) + "px; background-color: blue;");
            }
            else if(statue == 0x03)
            {
                ui->label_LED8->setStyleSheet("border-radius: " + QString::number(ui->label_LED8->width()/2) + "px; background-color: red;");
            }
            break;
        case ZFYX1_flag:
            if(statue == 0x01)
            {
                if(ZFYX1_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED9->setStyleSheet("border-radius: " + QString::number(ui->label_LED9->width()/2) + "px; background-color: red;");
                    ZFYX1_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED9->setStyleSheet("border-radius: " + QString::number(ui->label_LED9->width()/2) + "px; background-color: green;");
                    ZFYX1_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED9->setStyleSheet("border-radius: " + QString::number(ui->label_LED9->width()/2) + "px; background-color: green;");
            }
            else if(statue == 0x03)
            {
                ui->label_LED9->setStyleSheet("border-radius: " + QString::number(ui->label_LED9->width()/2) + "px; background-color: red;");
            }
            break;
        case ZFYX2_flag:
            if(statue == 0x01)
            {
                if(ZFYX2_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED10->setStyleSheet("border-radius: " + QString::number(ui->label_LED10->width()/2) + "px; background-color: red;");
                    ZFYX2_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED10->setStyleSheet("border-radius: " + QString::number(ui->label_LED10->width()/2) + "px; background-color: blue;");
                    ZFYX2_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED10->setStyleSheet("border-radius: " + QString::number(ui->label_LED10->width()/2) + "px; background-color: blue;");
            }
            else if(statue == 0x03)
            {
                ui->label_LED10->setStyleSheet("border-radius: " + QString::number(ui->label_LED10->width()/2) + "px; background-color: red;");
            }
            break;
        case YFYX1_flag:
            if(statue == 0x01)
            {
                if(YFYX1_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED11->setStyleSheet("border-radius: " + QString::number(ui->label_LED11->width()/2) + "px; background-color: red;");
                    YFYX1_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED11->setStyleSheet("border-radius: " + QString::number(ui->label_LED11->width()/2) + "px; background-color: green;");
                    YFYX1_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED11->setStyleSheet("border-radius: " + QString::number(ui->label_LED11->width()/2) + "px; background-color: green;");
            }
            else if(statue == 0x03)
            {
                ui->label_LED11->setStyleSheet("border-radius: " + QString::number(ui->label_LED11->width()/2) + "px; background-color: red;");
            }
            break;
        case YFYX2_flag:
            if(statue == 0x01)
            {
                if(YFYX2_ShowLEDStatus)
                {
                    // 初始设置为红色
                    ui->label_LED12->setStyleSheet("border-radius: " + QString::number(ui->label_LED12->width()/2) + "px; background-color: red;");
                    YFYX2_ShowLEDStatus = false;
                }
                else
                {
                    // 初始设置为绿色
                    ui->label_LED12->setStyleSheet("border-radius: " + QString::number(ui->label_LED12->width()/2) + "px; background-color: blue;");
                    YFYX2_ShowLEDStatus = true;
                }
            }
            else if(statue == 0x02)
            {
                ui->label_LED12->setStyleSheet("border-radius: " + QString::number(ui->label_LED12->width()/2) + "px; background-color: blue;");
            }
            else if(statue == 0x03)
            {
                ui->label_LED12->setStyleSheet("border-radius: " + QString::number(ui->label_LED12->width()/2) + "px; background-color: red;");
            }
            break;
        case Battery_flag:
            if(statue == 0x03)
            {
                // 初始设置为红色
                ui->label_LED13->setStyleSheet("border-radius: " + QString::number(ui->label_LED13->width()/2) + "px; background-color: red;");

            }
            else if(statue == 0x02)
            {
                ui->label_LED13->setStyleSheet("border-radius: " + QString::number(ui->label_LED13->width()/2) + "px; background-color: green;");
            }
            break;
        case Pod_flag:
            if(statue == 0x01)
            {
                ui->PodButton->setStyleSheet("background-color: green;");
            }
            else if(statue == 0x00)
            {
                ui->PodButton->setStyleSheet("background-color: red;");
            }
            break;
        case CT_flag:
            if(statue == 0x01)
            {
                ui->CTButton->setStyleSheet("background-color: green;");
            }
            else if(statue == 0x00)
            {
                ui->CTButton->setStyleSheet("background-color: red;");
            }
            break;
        case FDJ_flag:
            if(statue == 0x01)
            {
                ui->FDJButton->setStyleSheet("background-color: green;");
            }
            else if(statue == 0x00)
            {
                ui->FDJButton->setStyleSheet("background-color: red;");
            }
            break;
        default:

            break;
    }

}

/*
    左机臂伸展
*/
void ZD60_Control::on_ZJB1_clicked()
{
    send_command_packet(0x01);
}

/*
    左机臂折叠
*/
void ZD60_Control::on_ZJB2_clicked()
{
    send_command_packet(0x02);
}


/*
    右机臂伸展
*/
void ZD60_Control::on_YJB1_clicked()
{
    send_command_packet(0x03);
}

/*
    右机臂折叠
*/
void ZD60_Control::on_YJB2_clicked()
{
    send_command_packet(0x04);
}

/*
    左幅翼前伸展
*/
void ZD60_Control::on_ZFYC1_clicked()
{
    send_command_packet(0x05);
}

/*
    左幅翼前折叠
*/
void ZD60_Control::on_ZFYC2_clicked()
{
    send_command_packet(0x06);
}


/*
    左幅翼后伸展
*/
void ZD60_Control::on_ZFYX1_clicked()
{
    send_command_packet(0x09);
}

/*
    左幅翼后折叠
*/
void ZD60_Control::on_ZFYX2_clicked()
{
    send_command_packet(0x0A);
}

/*
    右幅翼前伸展
*/
void ZD60_Control::on_YFYC1_clicked()
{
    send_command_packet(0x07);
}

/*
    右幅翼前折叠
*/
void ZD60_Control::on_YFYC2_clicked()
{
    send_command_packet(0x08);
}

/*
    右幅翼后伸展
*/
void ZD60_Control::on_YFYX1_clicked()
{
    send_command_packet(0x0B);
}

/*
    右幅翼后折叠
*/
void ZD60_Control::on_YFYX2_clicked()
{
    send_command_packet(0x0C);
}
/*
    所有都折叠
*/
void ZD60_Control::on_ALL_IN_clicked()
{
    send_command_packet(0x0D);

}
/*
    所有都伸展
*/
void ZD60_Control::on_ALL_OUT_clicked()
{
    send_command_packet(0x0E);

}

void ZD60_Control::on_ALL_STOP_clicked()
{
    send_command_packet(0x0F);
    ui->label_LED2->setStyleSheet("border-radius: " + QString::number(ui->label_LED2->width()/2) + "px; background-color: red;");
    ui->label_LED4->setStyleSheet("border-radius: " + QString::number(ui->label_LED4->width()/2) + "px; background-color: red;");
    ui->label_LED5->setStyleSheet("border-radius: " + QString::number(ui->label_LED5->width()/2) + "px; background-color: red;");
    ui->label_LED6->setStyleSheet("border-radius: " + QString::number(ui->label_LED6->width()/2) + "px; background-color: red;");
    ui->label_LED7->setStyleSheet("border-radius: " + QString::number(ui->label_LED7->width()/2) + "px; background-color: red;");
    ui->label_LED8->setStyleSheet("border-radius: " + QString::number(ui->label_LED8->width()/2) + "px; background-color: red;");
    ui->label_LED9->setStyleSheet("border-radius: " + QString::number(ui->label_LED9->width()/2) + "px; background-color: red;");
    ui->label_LED10->setStyleSheet("border-radius: " + QString::number(ui->label_LED10->width()/2) + "px; background-color: red;");
    ui->label_LED11->setStyleSheet("border-radius: " + QString::number(ui->label_LED11->width()/2) + "px; background-color: red;");
    ui->label_LED12->setStyleSheet("border-radius: " + QString::number(ui->label_LED12->width()/2) + "px; background-color: red;");
    ui->label_LED1->setStyleSheet("border-radius: " + QString::number(ui->label_LED1->width()/2) + "px; background-color: red;");
    ui->label_LED3->setStyleSheet("border-radius: " + QString::number(ui->label_LED3->width()/2) + "px; background-color: red;");
    ui->label_LED13->setStyleSheet("border-radius: " + QString::number(ui->label_LED13->width()/2) + "px; background-color: red;");
}
/*
    吊舱供电
*/
void ZD60_Control::on_PodButton_clicked()
{
    if(PowerStatus)
    {
        send_command_packet(0x10);
        // ui->PodButton->setStyleSheet("background-color: green;");
        PowerStatus = false;
    }
    else
    {
        send_command_packet(0x11);
        // ui->PodButton->setStyleSheet("background-color: red;");
        PowerStatus = true;
    }



}
/*
    磁探设备供电
*/
void ZD60_Control::on_CTButton_clicked()
{
    if(PowerStatus)
    {
        send_command_packet(0x20);

        PowerStatus = false;
    }
    else
    {
        send_command_packet(0x21);
        // ui->CTButton->setStyleSheet("background-color: red;");
        PowerStatus = true;
    }
}
/*
    发动机加热供电
*/
void ZD60_Control::on_FDJButton_clicked()
{
    if(PowerStatus)
    {
        send_command_packet(0x30);
        // ui->FDJButton->setStyleSheet("background-color: green;");
        PowerStatus = false;
    }
    else
    {
        send_command_packet(0x31);
        // ui->FDJButton->setStyleSheet("background-color: red;");
        PowerStatus = true;
    }
}

void ZD60_Control::on_Read_statue_clicked()
{

    send_statue_command(0x66);
}
